// vim: sw=4:ts=4:nu:nospell:fdc=4
/**
 * Simple Message Bus Example
 *
 * @author    Ing. Jozef Sakáloš
 * @copyright (c) 2009, by Ing. Jozef Sakáloš
 * @date      19. September 2008
 * @version   $Id: simplebus.js 156 2009-09-19 23:31:02Z jozo $
 *
 * @license simplebus.js is licensed under the terms of the Open Source
 * LGPL 3.0 license. Commercial use is permitted to the extent that the 
 * code/component(s) do NOT become part of another Open Source or Commercially
 * licensed development library or toolkit without explicit permission.
 * 
 * License details: http://www.gnu.org/licenses/lgpl.html
 */
 
/*global Ext,Example */
Ext.ns('Example');
 
Ext.BLANK_IMAGE_URL = 'ext3/resources/images/default/s.gif';

// panel that sends and receives bus messages
Example.Panel = Ext.extend(Ext.Panel, {
	 autoScroll:true
	,bodyStyle:'font-size:12px'
	,subText:'**'
	,pubText:'foo'
	,msgCounter:0
	,initComponent:function() {
		var config = {
			 plugins:['msgbus']
			,tbar:{
				 itemId:'tbar'
				,items:[{
					 xtype:'label'
					,text:'Subject: eu.extjs.'
				},{
					 xtype:'textfield'
					,value:this.subText
					,itemId:'subtext'
					,width:80
				},'->',{
					  xtype:'button'
					 ,text:'Clear Log'
					 ,scope:this
					 ,handler:function() {
						 this.msgCounter = 0;
						 this.body.update('');
					 }
				}, {
					 xtype:'button'
					,text:'Subscribe'
					,scope:this
					,handler:function() {
						var textField = this.getTopToolbar().getComponent('subtext');
						var subject = 'eu.extjs.' + textField.getValue();
						if(false === this.subscribe(subject)) {
							this.infoBox(subject, 'Already subscribed to');
						}
					}
				},{
					 xtype:'button'
					,text:'Unsubscribe'
					,scope:this
					,handler:function() {
						var textField = this.getTopToolbar().getComponent('subtext');
						var subject = 'eu.extjs.' + textField.getValue();
						if(false === this.unsubscribe(subject)) {
							this.infoBox(subject, 'Not subscribed to');
						}
					}
				}]
			}

			,bbar:{
				 itemId:'bbar'
				,items:[{
					 xtype:'label'
					,text:'Subject: eu.extjs.'
				},{
					 xtype:'textfield'	
					,itemId:'msgsubj'
					,width:80
					,value:this.pubText
				},{
					 xtype:'label'
					,text:'Message: '
					,style:'margin:0 0 0 10px'
				},{
					 xtype:'textfield'	
					,itemId:'msgtext'
					,width:80
					,value:'Content'
				},'->',{
					 xtype:'button'
					,text:'Publish'
					,scope:this
					,handler:function() {
						var msgField = this.getBottomToolbar().getComponent('msgtext');
						var subField = this.getBottomToolbar().getComponent('msgsubj');
						this.publish('eu.extjs.' + subField.getValue(), msgField.getValue());
					}
				}]
			}
		}; // eo config
		Ext.apply(this, Ext.apply(this.initialConfig, config));
		Example.Panel.superclass.initComponent.apply(this, arguments);
	}
	,onMessage:function(subject, message) {
		this.body.createChild({html:String.format(
			 '<i>Received [{0}]:</i> Subject: <b>{1}</b>, Message: <b>{2}</b>'
			,++this.msgCounter, subject, message
		)});
		this.body.scrollTo('t', 100000);
	} // eo function onMessage

	,infoBox:function(subject, text) {
		Ext.MessageBox.show({
			 title:'Information'
			,msg:text + ' <b>' + subject + '</b>' 
				+ '<br><br>Subscriptions:<br>'
				+ this.listSubscriptions()
			,icon:Ext.MessageBox.INFO
			,buttons:Ext.MessageBox.OK
		});
	} // eo function infoBox

	,listSubscriptions:function() {
		var msg = [];
		var subs = this.getSubscriptions();
		for(sub in subs) {
			msg.push(sub)
		}
		return msg.join('<br>');
	} // eo function listSubscriptions

}); // eo class Example.Panel

// app entry point
Ext.onReady(function() {

	var c1 = new Example.Panel({
		 title:'Subscriber | Publisher 1'
		,renderTo:Ext.getBody()
		,subText:'foo'
		,pubText:'bar'
		,id:'c1'
		,width:400
		,height:160
		,html:'<div style="margin:6px;font-size:13px"><i>Quick Start:</i> Click Subscribe on all three panels, then start clicking Publish and watch messages. Keep playing and enjoy!</div>'
	});

	var c2 = new Example.Panel({
		 title:'Subscriber | Publisher 2'
		,renderTo:Ext.getBody()
		,subText:'bar'
		,pubText:'foo'
		,id:'c2'
		,width:400
		,height:160
	});

	var c3 = new Example.Panel({
		 title:'Subscriber | Publisher 3'
		,renderTo:Ext.getBody()
		,pubText:'foo'
		,id:'c3'
		,width:400
		,height:160
	});

}); // eo onReady
 
// eof
